<template>
  <Dialog
    :visible="visible"
    modal
    :header="dialogTitle"
    :style="{ width: '400px' }"
    :closable="true"
    :draggable="false"
    @update:visible="$emit('update:visible', $event)"
    @hide="handleClose"
    class="confirm-dialog"
  >
    <div class="confirm-content">
      <!-- 图标和消息 -->
      <div class="message-container">
        <div class="icon-wrapper" :class="typeClass">
          <i :class="iconClass"></i>
        </div>
        <div class="message-text">
          <h4 class="message-title">{{ title }}</h4>
          <p class="message-description">{{ message }}</p>
        </div>
      </div>
    </div>

    <!-- 底部按钮 -->
    <template #footer>
      <div class="dialog-footer">
        <Button
          label="取消"
          severity="secondary"
          size="small"
          @click="handleCancel"
          class="cancel-btn"
        />
        <Button
          :label="confirmText"
          :severity="buttonSeverity"
          size="small"
          @click="handleConfirm"
          class="confirm-btn"
        />
      </div>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import Dialog from 'primevue/dialog'
import Button from 'primevue/button'

interface Props {
  visible: boolean
  type?: 'info' | 'danger'
  title?: string
  message?: string
  confirmText?: string
  cancelText?: string
}

interface Emits {
  (e: 'update:visible', value: boolean): void
  (e: 'confirm'): void
  (e: 'cancel'): void
}

const props = withDefaults(defineProps<Props>(), {
  type: 'info',
  title: '确认操作',
  message: '您确定要执行此操作吗？',
  confirmText: '确定',
  cancelText: '取消'
})

const emit = defineEmits<Emits>()

// 计算属性
const dialogTitle = computed(() => {
  return props.type === 'danger' ? '危险操作' : '信息确认'
})

const typeClass = computed(() => {
  return {
    'info-type': props.type === 'info',
    'danger-type': props.type === 'danger'
  }
})

const iconClass = computed(() => {
  return props.type === 'danger' ? 'pi pi-exclamation-triangle' : 'pi pi-info-circle'
})

const buttonSeverity = computed(() => {
  return props.type === 'danger' ? 'danger' : 'primary'
})

// 事件处理
const handleConfirm = () => {
  emit('confirm')
  emit('update:visible', false)
}

const handleCancel = () => {
  emit('cancel')
  emit('update:visible', false)
}

const handleClose = () => {
  emit('update:visible', false)
}
</script>

<style scoped>
.confirm-dialog {
  border-radius: 12px;
  overflow: hidden;
}

.confirm-dialog :deep(.p-dialog-header) {
  background: #f8f9fa;
  border-bottom: 1px solid #e9ecef;
  padding: 20px 24px;
}

.confirm-dialog :deep(.p-dialog-title) {
  font-size: 18px;
  font-weight: 600;
  color: #212529;
}

.confirm-dialog :deep(.p-dialog-content) {
  padding: 24px;
  background: white;
}

.confirm-dialog :deep(.p-dialog-footer) {
  background: #f8f9fa;
  border-top: 1px solid #e9ecef;
  padding: 16px 24px;
}

.confirm-content {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.message-container {
  display: flex;
  align-items: flex-start;
  gap: 16px;
}

.icon-wrapper {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  margin-top: 4px;
}

.icon-wrapper.info-type {
  background: #e3f2fd;
  color: #1976d2;
}

.icon-wrapper.danger-type {
  background: #ffebee;
  color: #d32f2f;
}

.icon-wrapper i {
  font-size: 24px;
}

.message-text {
  flex: 1;
  min-width: 0;
}

.message-title {
  font-size: 16px;
  font-weight: 600;
  color: #212529;
  margin: 0 0 8px 0;
  line-height: 1.4;
}

.message-description {
  font-size: 14px;
  color: #6c757d;
  margin: 0;
  line-height: 1.5;
  word-break: break-word;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

.cancel-btn {
  min-width: 80px;
}

.confirm-btn {
  min-width: 80px;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .confirm-dialog {
    width: 90vw !important;
    margin: 0 auto;
  }
  
  .confirm-dialog :deep(.p-dialog-header),
  .confirm-dialog :deep(.p-dialog-content),
  .confirm-dialog :deep(.p-dialog-footer) {
    padding: 16px;
  }
  
  .message-container {
    gap: 12px;
  }
  
  .icon-wrapper {
    width: 40px;
    height: 40px;
  }
  
  .icon-wrapper i {
    font-size: 20px;
  }
  
  .message-title {
    font-size: 15px;
  }
  
  .message-description {
    font-size: 13px;
  }
  
  .dialog-footer {
    flex-direction: column;
    gap: 8px;
  }
  
  .cancel-btn,
  .confirm-btn {
    width: 100%;
  }
}
</style>
